package GUI;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * Główne okienko programu.
 * @author Piotrek
 */
public class MainDialog extends javax.swing.JDialog {

    // aktualny panel
    private int currentDisplayNum = 1;
    // osobnik
    private double[][] specimenPrototypeDouble;
    private engine.Enviroment env;
    private GraphAboutBox graphAboutBox = new GraphAboutBox(this, true);
    // edytor grafów
    private GraphEditor edytor;
    // wykresy
    private DynamicGraph graph = new DynamicGraph();
    // GUI menu/buttony on/off
    boolean GUIenabled = env.isRunning();
    // wątek liczący
    private Thread envThread;

    /** Konstruktor MainDialog
     * @param parent podstawowe okno aplikacji z którego GraphAboutBox jest wyświetlany
     * @param modal podstawowe okno aplikacji nieaktywne - true lub aktywne - false podczas wyświetlania GraphAboutBox
     */
    public MainDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        btnStart.setVisible(false);
        graphAboutBox.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        panel1 = new javax.swing.JPanel();
        panel2 = new javax.swing.JPanel();
        fldSize = new javax.swing.JTextField();
        lblSize = new javax.swing.JLabel();
        lblMaxIterations = new javax.swing.JLabel();
        fldMaxIterations = new javax.swing.JTextField();
        lblGoodEnough = new javax.swing.JLabel();
        fldGoodEnough = new javax.swing.JTextField();
        lblEliteSize = new javax.swing.JLabel();
        fldEliteSize = new javax.swing.JTextField();
        lblStop = new javax.swing.JLabel();
        lblParams = new javax.swing.JLabel();
        lblM1 = new javax.swing.JLabel();
        fldM1 = new javax.swing.JTextField();
        lblM2 = new javax.swing.JLabel();
        fldM2 = new javax.swing.JTextField();
        lblA = new javax.swing.JLabel();
        fldA = new javax.swing.JTextField();
        lblB = new javax.swing.JLabel();
        fldB = new javax.swing.JTextField();
        panel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtOutput = new javax.swing.JTextArea();
        graphPanel = new javax.swing.JPanel();
        panel4 = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        backButton = new javax.swing.JButton();
        nextButton = new javax.swing.JButton();
        btnStart = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        fileJmenu = new javax.swing.JMenu();
        newMenuItem = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        aboutMenu = new javax.swing.JMenu();
        helpMenuItem = new javax.swing.JMenuItem();
        aboutMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Problem kolorowania krawędziowego grafu"); // NOI18N
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        mainPanel.setLayout(new java.awt.CardLayout());

        panel1.setLayout(new java.awt.BorderLayout());

        edytor = GraphEditor.make();
        panel1.add(edytor);
        panel1.revalidate();

        mainPanel.add(panel1, "1");

        fldSize.setText("1000"); // NOI18N
        fldSize.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fldSizeActionPerformed(evt);
            }
        });

        lblSize.setText("Wielkość populacji:"); // NOI18N

        lblMaxIterations.setText("Maksymalna ilość iteracji:"); // NOI18N

        fldMaxIterations.setText("100"); // NOI18N

        lblGoodEnough.setText("Oczekiwana ilość kolorów:"); // NOI18N

        fldGoodEnough.setText("0"); // NOI18N

        lblEliteSize.setText("Wielkość elity:"); // NOI18N

        fldEliteSize.setText("10"); // NOI18N

        lblStop.setText("WARUNKI STOPU:"); // NOI18N

        lblParams.setText("PARAMETRY ALGORYTMU:"); // NOI18N

        lblM1.setText("Parametr m1:"); // NOI18N

        fldM1.setText("1"); // NOI18N

        lblM2.setText("Parametr m2:"); // NOI18N

        fldM2.setText("2"); // NOI18N

        lblA.setText("Parametr a:"); // NOI18N

        fldA.setText("0.0001"); // NOI18N

        lblB.setText("Parametr b:"); // NOI18N

        fldB.setText("2"); // NOI18N

        org.jdesktop.layout.GroupLayout panel2Layout = new org.jdesktop.layout.GroupLayout(panel2);
        panel2.setLayout(panel2Layout);
        panel2Layout.setHorizontalGroup(
            panel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(panel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(panel2Layout.createSequentialGroup()
                        .add(panel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(panel2Layout.createSequentialGroup()
                                .add(panel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(panel2Layout.createSequentialGroup()
                                        .add(panel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                            .add(lblMaxIterations, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 170, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                            .add(lblGoodEnough))
                                        .add(24, 24, 24)
                                        .add(panel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                            .add(fldGoodEnough, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
                                            .add(fldMaxIterations, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)))
                                    .add(lblParams)
                                    .add(panel2Layout.createSequentialGroup()
                                        .add(panel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                            .add(lblSize, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 150, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                            .add(lblEliteSize)
                                            .add(lblM1))
                                        .add(44, 44, 44)
                                        .add(panel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                            .add(fldEliteSize)
                                            .add(fldM1)
                                            .add(fldM2, 0, 0, Short.MAX_VALUE)
                                            .add(fldA, 0, 0, Short.MAX_VALUE)
                                            .add(fldSize, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
                                            .add(fldB, 0, 0, Short.MAX_VALUE))))
                                .add(258, 258, 258))
                            .add(panel2Layout.createSequentialGroup()
                                .add(lblM2)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 189, Short.MAX_VALUE))
                            .add(panel2Layout.createSequentialGroup()
                                .add(lblA)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 197, Short.MAX_VALUE)))
                        .add(230, 230, 230))
                    .add(lblB)
                    .add(panel2Layout.createSequentialGroup()
                        .add(lblStop)
                        .addContainerGap(548, Short.MAX_VALUE))))
        );
        panel2Layout.setVerticalGroup(
            panel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(lblParams)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(panel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblSize)
                    .add(fldSize, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(panel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblEliteSize)
                    .add(fldEliteSize, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(panel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblM1)
                    .add(fldM1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(panel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblM2)
                    .add(fldM2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(panel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblA)
                    .add(fldA, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(panel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblB)
                    .add(fldB, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(45, 45, 45)
                .add(lblStop)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(panel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblMaxIterations)
                    .add(fldMaxIterations, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(panel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblGoodEnough)
                    .add(fldGoodEnough, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(76, Short.MAX_VALUE))
        );

        mainPanel.add(panel2, "2");

        txtOutput.setColumns(20);
        txtOutput.setRows(5);
        jScrollPane1.setViewportView(txtOutput);

        graphPanel.setLayout(new java.awt.BorderLayout());

        graph.setSize(graphPanel.getSize());
        graphPanel.add(graph, BorderLayout.CENTER);
        graphPanel.revalidate();
        engine.Enviroment.graph = graph;

        org.jdesktop.layout.GroupLayout panel3Layout = new org.jdesktop.layout.GroupLayout(panel3);
        panel3.setLayout(panel3Layout);
        panel3Layout.setHorizontalGroup(
            panel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panel3Layout.createSequentialGroup()
                .addContainerGap()
                .add(panel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, graphPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 626, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 626, Short.MAX_VALUE))
                .addContainerGap())
        );
        panel3Layout.setVerticalGroup(
            panel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panel3Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 105, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(graphPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE)
                .addContainerGap())
        );

        mainPanel.add(panel3, "3");

        panel4.setLayout(new java.awt.BorderLayout());
        mainPanel.add(panel4, "4");

        backButton.setText("Wstecz"); // NOI18N
        backButton.setEnabled(false);
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });

        nextButton.setText("Dalej"); // NOI18N
        nextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonActionPerformed(evt);
            }
        });

        btnStart.setText("START"); // NOI18N
        btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(evt);
            }
        });

        fileJmenu.setText("Plik"); // NOI18N

        newMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        newMenuItem.setText("Nowy graf"); // NOI18N
        newMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newMenuItemActionPerformed(evt);
            }
        });
        fileJmenu.add(newMenuItem);

        exitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
        exitMenuItem.setText("Zakończ"); // NOI18N
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileJmenu.add(exitMenuItem);

        menuBar.add(fileJmenu);

        aboutMenu.setText("Pomoc"); // NOI18N

        helpMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        helpMenuItem.setText("Pomoc"); // NOI18N
        helpMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpMenuItemActionPerformed(evt);
            }
        });
        aboutMenu.add(helpMenuItem);

        aboutMenuItem.setText("O programie..."); // NOI18N
        aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutMenuItemActionPerformed(evt);
            }
        });
        aboutMenu.add(aboutMenuItem);

        menuBar.add(aboutMenu);

        setJMenuBar(menuBar);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 646, Short.MAX_VALUE)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(btnStart, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 107, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 302, Short.MAX_VALUE)
                .add(backButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 110, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(nextButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 101, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 646, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(mainPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(7, 7, 7)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(nextButton)
                    .add(backButton)
                    .add(btnStart))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Przycisk Start
     * @param evt zdarzenie
     */
    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed
        // przerwanie obliczeń
        if (env.isRunning()) {
            envThread.stop();
            envThread = null;
            env.setRunning(false);
            enableGUI(true);
            return;
        }

        // wczytuje parametry symulacji:
        try {
            engine.Enviroment.populationSize = Integer.decode(fldSize.getText());
            engine.Enviroment.eliteSize = Integer.decode(fldEliteSize.getText());
            engine.Enviroment.m1 = Integer.decode(fldM1.getText());
            engine.Enviroment.m2 = Integer.decode(fldM2.getText());
            engine.Enviroment.a = Double.parseDouble(fldA.getText());
            engine.Enviroment.b = Double.parseDouble(fldB.getText());

            // warunki stopu algorytmu:
            engine.Enviroment.maxIterations = Integer.decode(fldMaxIterations.getText());
            engine.Enviroment.goodEnough = Integer.decode(fldGoodEnough.getText());
        } catch (Exception any) {
            // komunikat o błędzie
            JOptionPane.showMessageDialog(this, "Proszę wrócić do parametrów i podać prawidłowe wartości.");
            return;
        }

        if (!parametersOK()) {
            JOptionPane.showMessageDialog(this, "Proszę wrócić do parametrów i podać prawidłowe wartości.");
            return;
        }

        enableGUI(false);


        // nowy wykres
        graph.clearGraph();
        // konwersja macierzy double na byte
        byte[][] specimenPrototype = new byte[specimenPrototypeDouble.length][specimenPrototypeDouble.length];
        for (int i = 0; i < specimenPrototype.length; i++) {
            for (int j = 0; j < specimenPrototype.length; j++) {
                if (specimenPrototypeDouble[i][j] != 0) {
                    specimenPrototype[i][j] = 1;
                }
            }
        }
        // dane wej. do engine.Enviroment (obliczenia)
        engine.Enviroment.specimenPrototype = specimenPrototype;
        // wydruk z symulacji
        engine.Enviroment.txtOutput = txtOutput;
        UpdateThread updThread = new UpdateThread();
        engine.Enviroment.guiThread = new Thread(updThread);


        env = new engine.Enviroment();
        // wątek z obliczeniami
        envThread = new Thread(env);
        envThread.start();

    }//GEN-LAST:event_btnStartActionPerformed
    /**
     * Przycisk Dalej
     * @param evt zdarzenie
     */
    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextButtonActionPerformed
        //wskazuje panel do wyswietlenia
        CardLayout cl = (CardLayout) (mainPanel.getLayout());
        String card = String.valueOf(currentDisplayNum + 1);

        if (currentDisplayNum == 1) {
            //przełącza na drugi ekran
            edu.uci.ics.jung.graph.decorators.Indexer.getAndUpdateIndexer(edytor.getGraph());
            specimenPrototypeDouble = edu.uci.ics.jung.algorithms.GraphMatrixOperations.graphToSparseMatrix(edytor.getGraph()).toArray();
            //gdy graf. wejściowy błędny
            if (specimenPrototypeDouble.length == 0) {
                // komunikat o błędzie
                JOptionPane.showMessageDialog(this, "Proszę narysować graf. Brak danych do dalszych obliczeń.");
                return;
            }
            backButton.setEnabled(true);
        } else if (currentDisplayNum == 2) {
            // pokaż [start]
            btnStart.setVisible(true);
            // wył. "next" (dopóki wątek nie skończy liczyć)
            nextButton.setEnabled(false);
        } else if (currentDisplayNum == 3) {
            // ukryj [start]
            btnStart.setVisible(false);
            //ostatni ekran
            panel4.removeAll();
            //rysuje graf na podst. byte[][]

            if (engine.Enviroment.populationSize > 0) {
                panel4.add(GraphPanel.make(env.getBestSpecimen().getDNA()));
            } else {
                JOptionPane.showMessageDialog(this, "Symulacja zakończyła się błędem. " + "Proszę cofnąć się do początkowej edycji grafu.");
            }
            panel4.revalidate();

            nextButton.setText("Koniec");
        } else if (currentDisplayNum == 4) {
            //koniec
            dispose();
            System.exit(0);
        }

        //pokaz panel o nazwie card
        cl.show(mainPanel, card);
        //numer wyswietlanego ekranu
        currentDisplayNum++;
    }//GEN-LAST:event_nextButtonActionPerformed

    /**
     * Przycisk Wstecz
     * @param evt zdarzenie
     */
    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
        //wskazuje panel do wyswietlenia
        CardLayout cl = (CardLayout) (mainPanel.getLayout());
        String card = String.valueOf(currentDisplayNum - 1);
        if (currentDisplayNum == 4) {
            //skok z powr. do trzeciego panelu
            nextButton.setText("Dalej");
            nextButton.setEnabled(true);
            btnStart.setVisible(true);
        } else if (currentDisplayNum == 3) {
            //skok z powr. do drugiego panelu
            btnStart.setVisible(false);
            graph.clearGraph();
            nextButton.setEnabled(true);
        } else if (currentDisplayNum == 2) {
            //skok z powr. do pierwszego panelu
            backButton.setEnabled(false);
            nextButton.setEnabled(true);
        } else if (currentDisplayNum == 1) {
            //aktualnie pierwszy panel
        }
        //pokaz panel o nazwie card
        cl.show(mainPanel, card);
        //zmiana numeru aktualnie pokazywanego ekranu
        currentDisplayNum = currentDisplayNum > 1 ? (--currentDisplayNum) : 1;
    }//GEN-LAST:event_backButtonActionPerformed

    /**
     * Menu: Plik\Zakończ
     * @param evt zdarzenie
     */
    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        dispose();
        System.exit(0);
}//GEN-LAST:event_exitMenuItemActionPerformed

    /**
     * Menu: Pomoc\O programie...
     * @param evt zdarzenie
     */
    private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed
        graphAboutBox.setVisible(true);
}//GEN-LAST:event_aboutMenuItemActionPerformed
    /**
     * Menu: Plik\Npwy graf
     * @param evt zdarzeie
     */
    private void newMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newMenuItemActionPerformed
        backButton.setEnabled(false);
        nextButton.setEnabled(true);
        // nowy edytor
        panel1.removeAll();
        edytor = GraphEditor.make();
        panel1.add(edytor);
        panel1.revalidate();
        // nowy wykres
        graph.clearGraph();

        // reset danych z edytora
        specimenPrototypeDouble = edu.uci.ics.jung.algorithms.GraphMatrixOperations.graphToSparseMatrix(edytor.getGraph()).toArray();

        CardLayout cl = (CardLayout) (mainPanel.getLayout());
        currentDisplayNum = 1;
        String card = String.valueOf(currentDisplayNum);
        //pokaz panel nr 1
        cl.show(mainPanel, card);
    }//GEN-LAST:event_newMenuItemActionPerformed
    /**
     * Menu: Pomoc\Opis edytora
     * @param evt zdarzenie
     */
    private void helpMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpMenuItemActionPerformed
        GraphEditorHelp.showHelp();
}//GEN-LAST:event_helpMenuItemActionPerformed

private void fldSizeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fldSizeActionPerformed

}//GEN-LAST:event_fldSizeActionPerformed

    /*
     * Włącza/wyłącza guziki/menu w GUI na czas obliczeń.
     * @param isEnabled gdy true to GUI aktywne, gdy false GUI nieaktywne
     */
    private void enableGUI(boolean isEnabled) {

        // buttony
        backButton.setEnabled(isEnabled);
        nextButton.setEnabled(isEnabled);

        // menu
        fileJmenu.setEnabled(isEnabled);
        aboutMenu.setEnabled(isEnabled);

        // button: start
        if (isEnabled) {
            btnStart.setText("Start");
        } else {
            btnStart.setText("Przerwij");
        }
    }

    /**
     * Main
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainDialog(new javax.swing.JFrame(), true).setVisible(true);
            }
            
        });
    }
/**
 * Sprawdznie parametrów.
 * @return true - parametry ok, false - błędne parametry
 */
    private boolean parametersOK() {
        if (engine.Enviroment.a > 0 &
                engine.Enviroment.b > 0 &
                engine.Enviroment.m1 > 0 &
                engine.Enviroment.m2 > 0 &
                engine.Enviroment.populationSize > 0 &
                engine.Enviroment.eliteSize > 0 &
                engine.Enviroment.goodEnough >= 0 &
                engine.Enviroment.maxIterations > 0) {
            return true;
        }
        return false;
    }

    /**
     * Klasa wykorzystywana do wydruku.
     */
    private class UpdateThread implements Runnable {

        public void run() {
            enableGUI(true);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu aboutMenu;
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JButton backButton;
    private javax.swing.JButton btnStart;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileJmenu;
    private javax.swing.JTextField fldA;
    private javax.swing.JTextField fldB;
    private javax.swing.JTextField fldEliteSize;
    private javax.swing.JTextField fldGoodEnough;
    private javax.swing.JTextField fldM1;
    private javax.swing.JTextField fldM2;
    private javax.swing.JTextField fldMaxIterations;
    private javax.swing.JTextField fldSize;
    private javax.swing.JPanel graphPanel;
    private javax.swing.JMenuItem helpMenuItem;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblA;
    private javax.swing.JLabel lblB;
    private javax.swing.JLabel lblEliteSize;
    private javax.swing.JLabel lblGoodEnough;
    private javax.swing.JLabel lblM1;
    private javax.swing.JLabel lblM2;
    private javax.swing.JLabel lblMaxIterations;
    private javax.swing.JLabel lblParams;
    private javax.swing.JLabel lblSize;
    private javax.swing.JLabel lblStop;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem newMenuItem;
    private javax.swing.JButton nextButton;
    private javax.swing.JPanel panel1;
    private javax.swing.JPanel panel2;
    private javax.swing.JPanel panel3;
    private javax.swing.JPanel panel4;
    private javax.swing.JTextArea txtOutput;
    // End of variables declaration//GEN-END:variables
}
